<?php

namespace app\admin\model;

use think\Db;
use think\Model;
use traits\model\SoftDelete;

class Plan extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'plan';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'startime_text',
        'endtime_text',
        'state_text',
        'status_text',
        'g_id_text',
        'p_id_text',
        'project_id_text',
        'admin_text',
        'remarks_text',
    ];


    protected static function init()
    {
        self::afterInsert(function ($row) {
            $pk = $row->getPk();
            $row->getQuery()->where($pk, $row[$pk])->update(['weigh' => $row[$pk]]);
        });
    }


    public function getStateList()
    {
        return ['0' => __('State 0'), '1' => __('State 1')];
    }

    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1'), '2' => __('Status 2')];
    }

    public function admin()
    {
        return $this->belongsTo('admin', 'admin_id')->setEagerlyType(0);
    }
    public function getStartimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['startime']) ? $data['startime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getEndtimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['endtime']) ? $data['endtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getGIdTextAttr($value, $data){
        $title = self::where('id',$data['g_id'])->value('title');
        return $title?$title:'-';
    }

    public function getPIdTextAttr($value, $data){
        $title = self::where('id',$data['p_id'])->value('title');
        return $title?$title:'-';
    }

    public function getAdminTextAttr($value, $data){
        return Admin::where('id',$data['admin_id'])->value('nickname')?Admin::where('id',$data['admin_id'])->value('nickname'):'-';
    }

    protected function setStartimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

    protected function setEndtimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }

      //修改设备id
    protected function setProjectIdsAttr($value)
    {
        //有可能是excel提交的，有可能是添加的 都是字符串
        //只有数字和逗号
        $par = '/^[(0-9)(\,)+]+$/';
        if(!preg_match($par, $value)){
            $value = explode(',',$value);
            foreach($value as $v){
                $arr[] = Project::where('project_id',$v)->value('id');
            }
            return implode(',',$arr);
        }
        return $value;
    }

    public function getProjectIdTextAttr($value,$row){

        $arr_value = explode(',',$row['project_id']);
        foreach($arr_value as $v){
            $arr[] = Project::where('id',$v)->value('title');
        }
        return implode(',',$arr);
    }


    public function getRemarksTextAttr($value, $data){
        return strip_tags($data['remarks']);
    }


    //获取日、周、月起止时间
    public function startEndTime($type) {
        $nowTime = time();
        if($type == 0){
            return $this->getDayTime($nowTime);
        }
        if($type == 1){
            return $this->getWeekTime($nowTime);
        }
        if($type == 2){
            return $this->getMonthTime($nowTime);
        }
        return "";
    }


    //日起止时间
    protected function getDayTime($nowTime){
        $format="Y-m-d";
        $startTime = date( 'Y-m-d 09:00:00', $nowTime );
        $endTime= date( 'Y-m-d 17:30:00' , $nowTime );
        return ['startTime'=>$startTime,'endTime'=>$endTime];
    }


    //周起止时间
    protected function getWeekTime($nowTime){
        $format="Y-m-d";
        //获取当前周几
        $week = date('w', $nowTime);
        if ($week==0) {
            $week=7;
        }
        $date = [];
        for ($i=1; $i<=7; $i++){
            $date[$i] = date($format ,strtotime( '+' . $i-$week .' days', $nowTime));
        }
        $startTime=$date[1] .' 09:00:00';
        $endTime=$date[7] .' 17:30:00';
        return ['startTime'=>$startTime,'endTime'=>$endTime];
    }


    //月起止时间
    protected function getMonthTime($nowTime){
        $mdays = date( 't', $nowTime );//获取当前月份天数
        $startTime = date( 'Y-m-01 09:00:00', $nowTime );
        $endTime= date( 'Y-m-' . $mdays.' 17:30:00' , $nowTime );
        return ['startTime'=>$startTime,'endTime'=>$endTime];
    }

    //获取信息
    public function getInfo($id=null,$g_id=null,$p_id=null,$project_id=null,$admin_id=null,$status=null,$order="weigh desc,id desc",$page=1,$pageCount=10) {
        $offset = ($page-1)*$pageCount;
        $where = [];
        if($id){$where['id'] = ['=',$id];}
        if($g_id){$where['g_id'] = ['=',$g_id];}
        if($p_id){$where['p_id'] = ['=',$p_id];}
        if($project_id){$where[] = ['exp',Db::raw("FIND_IN_SET($project_id,project_id)")];}
        if($admin_id){$where['admin_id'] = ['=',$admin_id];}
        if($status||$status==="0"){$where['status'] = ['=',$status];}
        $info = collection(self::where($where)->order($order)->limit($offset,$pageCount)->select())->toArray();
        return $info;
    }


}
